package com.wyu.controller;

import com.wyu.pojo.Page;
import com.wyu.pojo.User;
import com.wyu.pojo.menu;
import com.wyu.service.MenuService;
import com.wyu.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;


import javax.servlet.http.HttpSession;


@Controller
public class UserController {

    @Autowired
    @Qualifier("MenuServiceImpl")
    private MenuService menuService;
    @Autowired
    @Qualifier("UserServiceImpl")
    private UserService userService;

    @RequestMapping("/index123")
    public String index123() {
        return "index";
    }

    @RequestMapping("/tologin")
    public String tologin() {
        return "user/login";
    }

    @RequestMapping("/toregist")
    public String toregist() {
        return "user/regist";
    }

    @RequestMapping("/login")
    public String login(Model model, HttpSession httpSession, String username, String password) {

        // 调用 userService.login()登录处理业务
        User loginUser = userService.login(new User(null, username, password, null,null));
        System.out.println(loginUser);
        // 如果等于null,说明登录 失败!
        if (loginUser == null) {
            System.out.println("sss");
            // 把错误信息，和回显的表单项信息，保存到Request域中
            model.addAttribute("msg","用户或密码错误！");
            model.addAttribute("username", username);
            //   跳回登录页面
            return "user/login";
        } else {
            // 登录 成功
            // 保存用户登录的信息到Session域中
            httpSession.setAttribute("user", loginUser);
            System.out.println(loginUser.getId());
            if(loginUser.getRole()==1)
            {
                //跳到成功页面login_success.html
                System.out.println(loginUser.getUsername()+loginUser.getPassword());
                return "user/login_success";
            }else
            {
                return "manager/manager";
            }

        }
    }

    @RequestMapping("/regist")
    public String regist(Model model, String username, String password, String email) {
//        3、检查 用户名是否可用
        if (userService.existsUsername(username)) {
            System.out.println("用户名[" + username + "]已存在!");

            // 把回显信息，保存到Request域中
            model.addAttribute("msg", "用户名已存在！！");
            model.addAttribute("username", username);
            model.addAttribute("email", email);

//        跳回注册页面
            return "user/regist";
        } else {
            //      可用
//                调用Sservice保存到数据库
            userService.registUser(new User(null, username, password, email,1));
//
//        跳到注册成功页面 regist_success.jsp
            return "user/regist_success";
        }
    }


    @RequestMapping("/user123/menuindex/{pageNo}")
    public String menuindex(Model model, @PathVariable int pageNo) {
        Page<menu> page = menuService.page(pageNo,Page.PAGE_SIZE);
        page.setUrl("user123/menuindex");
        //3 保存Page对象到Request域中
        model.addAttribute("page",page);
        return "client/menuindex";
    }


    @GetMapping("/user123/menuindexByPrice")
    public String menuindexByPrice2(Model model, int pageNo ,  int min ,  int max) {
        Page<menu> page = menuService.pageByPrice(pageNo,Page.PAGE_SIZE,min,max);
        System.out.println(min+"yyyy"+max);
        StringBuilder sb = new StringBuilder("user123/menuindexByPrice");
        sb.append("/").append(min);
        sb.append("/").append(max);
        page.setUrl(sb.toString());
        System.out.println(sb);
        model.addAttribute("page",page);
        return "client/menuindex";
    }

    //http://localhost:8080/user123/2
    //http://localhost:8080/user123/menuindexByPrice?pageNo=1&min=23&max=70
    //http://localhost:8080/user123/menuindexByPrice///1?action=pageByPrice&min=23&max=70
    @GetMapping("/user123/menuindexByPrice/{min}/{max}/{pageNo}")
    public String menuindexByPrice(Model model,@PathVariable int pageNo , @PathVariable int min , @PathVariable int max) {
        Page<menu> page = menuService.pageByPrice(pageNo,Page.PAGE_SIZE,min,max);
        System.out.println(min+"yyyy"+max);
        StringBuilder sb = new StringBuilder("user123/menuindexByPrice");
        sb.append("/").append(min);
        sb.append("/").append(max);
        page.setUrl(sb.toString());
        model.addAttribute("page",page);
       return "client/menuindex";
    }

    @RequestMapping("/logout")
    public String logout(HttpSession httpSession) {
        //        1、销毁Session中用户登录的信息（或者销毁Session）
        httpSession.invalidate();
//        2、重定向到首页（或登录页面）。
        return "redirect:/index123";
    }

    @RequestMapping("/ajaxExistsUsername")
    @ResponseBody
    public String ajaxExistsUsername(String username )  {
        System.out.println(username);
        String msg = "";
        // 调用userService.existsUsername();
        boolean existsUsername = userService.existsUsername(username);
        // 把返回的结果封装成为map对象
        if(existsUsername){
            msg="用户名已存在!";
        }
        else{
            msg="用户名可用!";
        }
        return msg;//由于@RestController注解，将msg转成json格式返回
    }


}
